//
//  main.swift
//  gouzao
//
//  Created by henaihan on 2017/9/14.
//  Copyright © 2017年 henaihan. All rights reserved.
//构造10个学生（要求有学生的姓名、数学成绩、英语成绩），按照学生英语和数学平均分的成绩从小到大输出学生的姓名、数学成绩、英语成绩及平均分。

import Foundation

struct Student{
    var name:String
    var mathScore:Int
    var englishScore:Int
    var average:Float
    
    init(name:String,mathScore:Int,englishScore:Int)
    {
    self.name = name
    self.mathScore = mathScore
    self.englishScore = englishScore
    self.average = Float(englishScore+mathScore)/2
    
    }



}
func sortWithAverage(students:[Student])->[Student]{
    var result = students
    for i in 0...9{
        for j in i...9{
            if result[i].average < result[j].average{
            let temp = result[i]
            result[i] = result[j]
            result[j] = temp
            
            }
        
        
        }
    
    }
return result
}
var stu1 = Student(name: "stu01", mathScore: 83, englishScore: 99)
var stu2 = Student(name: "stu02", mathScore: 80, englishScore: 90)
var stu3 = Student(name: "stu03", mathScore: 60, englishScore: 76)
var stu4 = Student(name: "stu04", mathScore: 70, englishScore: 65)
var stu5 = Student(name: "stu05", mathScore: 90, englishScore: 39)
var stu6 = Student(name: "stu06", mathScore: 40, englishScore: 57)
var stu7 = Student(name: "stu07", mathScore: 50, englishScore: 45)
var stu8 = Student(name: "stu08", mathScore: 30, englishScore: 43)
var stu9 = Student(name: "stu09", mathScore: 23, englishScore: 49)
var stu10 = Student(name: "stu10", mathScore: 20, englishScore: 32)
var result = sortWithAverage(students: [stu1,
                                        stu2,
                                        stu3,
                                        stu4,
                                        stu5,
                                        stu6,
                                        stu7,
                                        stu8,
                                        stu9,
                                        stu10])

print("英语和数学平均分的成绩从小到大排列为:")
for i in 0..<result.count{
    print("姓名:\(result[i].name),数学成绩:\(result[i].mathScore),英语成绩:\(result[i].englishScore),平均分:\(result[i].average)")
}
